﻿
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link href="~/layui/css/layui.css" rel="stylesheet" />
    <link href="~/layuimini/css/public.css" rel="stylesheet" />
    <style>
      td .layui-table-cell{
    height:100px;
    line-height: 100px;
}
    </style>
</head>
<body>
    <div class="layuimini-container">
        <div class="layuimini-main">
            <fieldset class="table-search-fieldset">
                <legend>搜索信息</legend>
                <div style="margin: 10px 10px 10px 10px">
                    <form class="layui-form layui-form-pane" action="">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">名称</label>
                                <div class="layui-input-inline">
                                    <input type="text" id="name" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <button type="submit" class="layui-btn layui-btn-primary" lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                            </div>
                        </div>
                    </form>
                </div>
            </fieldset>
            <script type="text/html" id="toolbarDemo">
                <div class="layui-btn-container">
                    <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                    <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>
                </div>
            </script>
            <table class="layui-hide" id="table" lay-filter="currentTableFilter"></table>
            <script type="text/html" id="currentTableBar">
                <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
                <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
            </script>
        </div>
    </div>
    <script src="~/layui/layui.js"></script>
    <script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        table.render({
            elem: '#table',
            method:'post',
            url: '@Url.Action("GoodsListAsync", "Default")',
            request: {
                pageName: 'PageIndex' //页码的参数名称，默认：page
               ,limitName: 'PageSize' //每页数据量的参数名，默认：limit
            },
            where: {
            Search: $('#name').val()
            },
        toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print'],
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'ID',  title: 'ID'},
                { field: 'Name', title: '名称', totalRowText:'合计'},
                {field: 'Unit',  title: '单位'},
                {field: 'InPrice', title: '进价'},
                {field: 'OutPrice', title: '售价'},
                { field: 'Qty', title: '库存' },
                {field: 'url', title: '预览',
                    templet: function(d){
                        return '<div><img  src= "'+d.url+'" alt="" height="90px"></a></div>';
                    },event :'preview'
                },
                 {
                     field: 'url', title: '预览',
                     templet: '#currentTableBar'
                 }
            ]],
            limits: [10, 20, 30, 50, 100],
            limit: 20,
            page: true,
            //skin: 'line',
            totalRow: true,
            height: 'full-170',
            //size:'sm',
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            //执行搜索重载
            table.reload('table', {
                 where: {
                     Search: $('#name').val()
                }
            }, 'data');
            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加用户',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '../page/table/add.html',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('table')
                    , data = checkStatus.data;
                layer.alert(JSON.stringify(data));
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {

                var index = layer.open({
                    title: '编辑用户',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '../page/table/edit.html',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (index) {
                    obj.del();
                    layer.close(index);
                });
            } else if (obj.event === 'preview') {

                layer.photos({
                    photos: {
                        "title": "", //相册标题
                        "id": 123, //相册id
                        "start": 0, //初始显示的图片序号，默认0
                        "data": [   //相册包含的图片，数组格式
                          {
                              "alt": "",
                              "pid": 1, //图片id
                              "src": obj.data.url, //原图地址
                              "thumb": obj.data.url //缩略图地址
                          }
                        ]
                    } //格式见API文档手册页
    , anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机
                });





                //console.log("preview");
                //var url = obj.data.url;
                //var width = 800;
                //var height = 600;
                //// 创建对象
                //var img = new Image();
                //// 改变图片的src
                //img.src = url;
                //// 判断是否有缓存
                //if(img.complete){
                //    // 打印
                //    width = img.width>width?width:img.width;
                //    height = img.height>height?height:img.height + 42;
                //}else{
                //    // 加载完成执行
                //    img.onload = function(){
                //        width = img.width>width?width:img.width;
                //        height = img.height>height?height:img.height + 42;
                //    }
                //}
                //width = width + 'px';
                //height = height + 'px';
                ////页面层
                //layer.open({
                //    title:'预览',
                //    type: 1,
                //    skin: 'layui-layer-rim', //加上边框
                //    area: [width, height], //宽高
                //    shadeClose: true, //开启遮罩关闭
                //    end: function (index, layero) {
                //        return false;
                //    },
                //    content: '<div style="text-align:center"><img src="'+url+'"  /></div>'
                //});
            }
        });

    });
    </script>
</body>
</html>